package common

import (
	"regexp"
	"strconv"
	"strings"
)

// 字符串形式的主键，只能由小写字母、数字、下划线组成
type Code string

func (c Code) Verify() bool {
	if ok, err := regexp.Match("^[0-9a-z_]{1,}&", []byte(c)); err != nil {
		return false
	} else {
		return ok
	}
}

// 多个id拼接成的字符串
type IDList string

func MakeIDList(data []int) IDList {
	ss := make([]string, 0)
	for _, id := range data {
		ss = append(ss, strconv.Itoa(id))
	}
	return IDList(strings.Join(ss, ","))
}

func (s IDList) Value() ([]int, error) {
	ss := strings.Split(string(s), ",")
	data := make([]int, 0)
	for _, _id := range ss {
		id, err := strconv.Atoi(_id)
		if err != nil {
			return nil, err
		}
		data = append(data, id)
	}
	return data, nil
}

func (s IDList) Value2() ([]int, error) {
	ss := strings.Split(string(s), ",")
	data := make([]int, 0)
	for _, _id := range ss {
		id, err := strconv.Atoi(_id)
		if err != nil {
			return nil, err
		}
		data = append(data, id)
	}
	return data, nil
}
